//
//  CollectDetailsViewController.swift
//  NDAi
//
//  Created by 丸子 on 2024/6/20.
//

import UIKit

class CollectDetailsViewController: BaseViewController {
    var model:AIMessage?
    
    private lazy var tableView: UITableView = {
        let tableView = UITableView(frame: .zero, style: .plain)
        tableView.delegate = self
        tableView.dataSource = self
        tableView.rowHeight = UITableView.automaticDimension
        tableView.register(cellWithClass: CollectTableViewCell.self)
        tableView.separatorStyle = .none
        tableView.backgroundColor = .clear
        tableView.showsVerticalScrollIndicator = false
        tableView.showsHorizontalScrollIndicator = false
//        tableView.contentInsetAdjustmentBehavior = .never
        return tableView
    }()
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.editNav(isCurrent: true)
    }
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        self.editNav(isCurrent: false)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor(hexString: "#F3F2F7")
        self.navigationItem.title = "详情"
        self.view.addSubview(tableView)
        self.tableView.snp.makeConstraints { make in
            make.top.equalTo(self.safeTop)
            make.bottom.equalTo(self.safeBottom)
            make.leading.trailing.equalToSuperview()
        }
        
        let rightBtn = UIButton(type: .custom)
        rightBtn.setTitle("删除", for: .normal)
        rightBtn.titleColorForNormal = UIColor(hexString: "#FF4E4E")
        rightBtn.titleLabel?.font = UIFont.pingFangSCRegular(ofSize: 15)
        rightBtn.contentEdgeInsets = UIEdgeInsets(top: 2, left: 12, bottom: 3, right: 12)
        let rightItem = UIBarButtonItem(customView: rightBtn)
        self.navigationItem.rightBarButtonItem = rightItem
        
        
        rightBtn.setClosureTouchUpInside_k { [weak self] in
            self?.deleteCollect()
        }
        
    }
    
    func deleteCollect(){
        if var model = self.model {
            model.collect = false
            SqlManager.shared.update(on: [AIMessage.Properties.collect], with: model, where: AIMessage.Properties.sort == model.sort ?? 0)
            self.popSelf(animation: true)
            NDProgressHUD.show(message: "删除成功")
        }
    }
}
extension CollectDetailsViewController: UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        1
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withClass: CollectTableViewCell.self, for: indexPath)
        cell.contentLb.numberOfLines = 0
        cell.contentLb.text = self.model?.message ?? ""
        return cell
    }
    
}
